﻿using Common.Contract.Authentication.Record;
using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Contract.Paging;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

namespace Customer.Core.ServiceCommand
{
    public class RetrieveCustomerTop10ServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveCustomerTop10Request, RetrieveCustomerTop10Response>
    {
        #region Properties
        public UserRecord Dao;
        public int Type;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        CultureInfo cul = CultureInfo.GetCultureInfo("vi-VN");   // try with "en-US"
        #endregion
        public RetrieveCustomerTop10ServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveCustomerTop10Request request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("MSBO Dao must be set", "request");
            }
            else
            {
                Dao = Request.CurrentUser;
            }
            if (request.Type != 1 && request.Type != 2)
            {
                throw new ArgumentException("Type not support", "request");
            }
            else
            {
                Type = request.Type;
            }
        }

        protected override bool OnExecute(RetrieveCustomerTop10Response response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<CustomerRecord> records = new List<CustomerRecord>();
            List<CustomerTop10> listCustomer = new List<CustomerTop10>();
            listCustomer = Providers.Customers.RetrieveCustomerTop10(Dao, Type, Request.Filter);
            Dictionary<string, BranchRecord> listBranches = Providers.Branchs.RetrieveAllBranches().Select(x => new BranchRecord
            {
                BranchId = x.BranchId,
                BranchName = x.BranchNameSme,
                Region = x.Region
            }).ToList().ToDictionary(x => x.BranchId);
            if (listCustomer != null)
            {
                records = listCustomer.Select(x => new CustomerRecord
                {
                    DaoName = x.DaoName,
                    Dao = x.DaoCif,
                    Branch = listBranches.ContainsKey(x.BranchId) ? listBranches[x.BranchId]:new BranchRecord(),
                    CIF = x.Cif,
                    Name = x.CusName,
                    VpbIndustry = x.VpbIndustry,
                    VpbIndustryEn = x.IndustryName,
                    Toi = x.Toi.ToString("#,###", cul.NumberFormat)
                }).ToList();
            }
            response.Records = records;
            response.Page = new PageRecord
            {
                PageIndex = pageIndex,
                PageSize = pageSize,
                TotalRecords = totalRecord
            };
            return isSuccess;
        }


    }
}
